package org.dczl.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import org.dczl.common.core.utils.MapstructUtils;
import org.dczl.common.core.utils.StringUtils;
import org.dczl.common.mybatis.core.page.TableDataInfo;
import org.dczl.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dczl.system.domain.DpLab;
import org.dczl.system.domain.vo.ExportDpProjectVo;
import org.dczl.system.mapper.DpLabMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.dczl.system.domain.bo.DpProjectBo;
import org.dczl.system.domain.vo.DpProjectVo;
import org.dczl.system.domain.DpProject;
import org.dczl.system.mapper.DpProjectMapper;
import org.dczl.system.service.IDpProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * 项目Service业务层处理
 *
 * @author jia
 * @date 2025-04-16
 */
@RequiredArgsConstructor
@Service
public class DpProjectServiceImpl implements IDpProjectService {

    private final DpProjectMapper baseMapper;

    private final DpLabMapper labMapper;

    /**
     * 查询项目
     */
    @Override
    public DpProjectVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(List<ExportDpProjectVo> result) {
        result.forEach(item -> {
            DpLab lab = labMapper.selectOne(Wrappers.lambdaQuery(DpLab.class).eq(DpLab::getName, item.getLabName()));
            if (Objects.isNull(lab)) {
                lab = new DpLab();
                lab.setName(item.getLabName());
                lab.setLocation(item.getLocation());
                lab.setType(item.getType());
                labMapper.insert(lab);
            }
            DpProject project = baseMapper.selectOne(Wrappers.lambdaQuery(DpProject.class)
                    .eq(DpProject::getProjectName, item.getProjectName())
                    .eq(DpProject::getLabId, lab.getId())
                    .eq(DpProject::getStartTime, item.getStartTime())
                    .eq(DpProject::getWeek, item.getWeek()));
            if (Objects.isNull(project)) {
                project = new DpProject();
                project = BeanUtil.copyProperties(item, DpProject.class);
                project.setLabId(lab.getId());
                project.setStartTime(DateUtil.formatDate(DateUtil.parse(item.getStartTime())));
                baseMapper.insert(project);
            }
        });
    }

    /**
     * 查询项目列表
     */
    @Override
    public TableDataInfo<DpProjectVo> queryPageList(DpProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DpProject> lqw = buildQueryWrapper(bo);
        Page<DpProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目列表
     */
    @Override
    public List<DpProjectVo> queryList(DpProjectBo bo) {
        LambdaQueryWrapper<DpProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DpProject> buildQueryWrapper(DpProjectBo bo) {
        LambdaQueryWrapper<DpProject> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLabId() != null, DpProject::getLabId, bo.getLabId());
        lqw.eq(StringUtils.isNotBlank(bo.getTeacher()), DpProject::getTeacher, bo.getTeacher());
        lqw.like(StringUtils.isNotBlank(bo.getCourseName()), DpProject::getCourseName, bo.getCourseName());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), DpProject::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectType()), DpProject::getProjectType, bo.getProjectType());
        lqw.eq(bo.getProjectNum() != null, DpProject::getProjectNum, bo.getProjectNum());
        lqw.eq(bo.getGroupNum() != null, DpProject::getGroupNum, bo.getGroupNum());
        lqw.eq(bo.getGroupPeopleNum() != null, DpProject::getGroupPeopleNum, bo.getGroupPeopleNum());
        lqw.eq(StringUtils.isNotBlank(bo.getMajor()), DpProject::getMajor, bo.getMajor());
        lqw.eq(StringUtils.isNotBlank(bo.getClasses()), DpProject::getClasses, bo.getClasses());
        lqw.eq(bo.getClassesPeopleNum() != null, DpProject::getClassesPeopleNum, bo.getClassesPeopleNum());
        lqw.eq(StringUtils.isNotBlank(bo.getWeek()), DpProject::getWeek, bo.getWeek());
        lqw.eq(StringUtils.isNotBlank(bo.getToday()), DpProject::getToday, bo.getToday());
        lqw.eq(bo.getPitchNum() != null, DpProject::getPitchNum, bo.getPitchNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPitch()), DpProject::getPitch, bo.getPitch());
        lqw.eq(StringUtils.isNotBlank(bo.getStartTime()), DpProject::getStartTime, bo.getStartTime());
        return lqw;
    }

    /**
     * 新增项目
     */
    @Override
    @Transactional
    public Boolean insertByBo(DpProjectBo bo) {
        DpProject add = MapstructUtils.convert(bo, DpProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改项目
     */
    @Override
    @Transactional
    public Boolean updateByBo(DpProjectBo bo) {
        DpProject update = MapstructUtils.convert(bo, DpProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DpProject entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目
     */
    @Override
    @Transactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
